from odoo import models, fields, api
from datetime import timedelta


class Session(models.Model):
    """Session Model"""
    _name = "openacademy.session"
    _description = "开课"
    _order = "name"

    name = fields.Char(string="开课名称", required=True)
    start_date = fields.Date(string="起始时间", default=fields.Date.today)
    end_date = fields.Date(string="截止时间", store=True, compute="")


    @api.depends('start_date', 'duration')
    def _get_end_date(self):
        """
            Compute End Date
            Add duration to start_date, but: Monday + 5 days = Saturday, so
            subtract one second to get on Friday instead
        """
        for r in self:
            if not (r.start_date and r.duration):
                r.end_date = r.start_date
            else:
                start = fields.Datetime.from_string(r.start_date)
                duration = timedelta(days = r.duration, seconds = -1)
                r.end_date = start + duration
